package com.summer.practice.nunu.annotataion;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * 协助类，旨在完成excel到dto的映射
 */
public class ExcelKit {

    private Map<String,Field> fieldMap=new HashMap<>();
    private String masterColumnKey;
    private String masterValue;
    private String slaveColumnKey;
    private int needValNum;

    private ExcelKit() {
    }

    /**
     * 组装excel辅助对象
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> ExcelKit assembleExcelHelper(Class<T>  clazz){
        if(null==clazz){
            return null;
        }
        ExcelKit excelKit =new ExcelKit();
        for (Field field:clazz.getDeclaredFields()) {
            ExcelColumn excelColumn=field.getAnnotation(ExcelColumn.class);
            if(null==excelColumn){
                continue;
            }

            excelKit.getFieldMap().put(excelColumn.column().name(),field);

            if(ColumnRole.master.equals(excelColumn.role())){
                excelKit.setMasterColumnKey(excelColumn.column().name());
                excelKit.setMasterValue(excelColumn.value());
            }else if(ColumnRole.slave.equals(excelColumn.role())){
                excelKit.setSlaveColumnKey(excelColumn.column().name());
            }

            if(excelColumn.needVal()){
                excelKit.setNeedValNum(excelKit.getNeedValNum()+1);
            }
        }
        return excelKit;
    }

    /**
     * 自检
     * @return
     */
    public static Boolean checkAnnotation(){
        return false;
    }


    public Map<String, Field> getFieldMap() {
        return fieldMap;
    }

    public void setFieldMap(Map<String, Field> fieldMap) {
        this.fieldMap = fieldMap;
    }

    public String getMasterColumnKey() {
        return masterColumnKey;
    }

    public void setMasterColumnKey(String masterColumnKey) {
        this.masterColumnKey = masterColumnKey;
    }

    public String getSlaveColumnKey() {
        return slaveColumnKey;
    }

    public void setSlaveColumnKey(String slaveColumnKey) {
        this.slaveColumnKey = slaveColumnKey;
    }

    public String getMasterValue() {
        return masterValue;
    }

    public void setMasterValue(String masterValue) {
        this.masterValue = masterValue;
    }

    public int getNeedValNum() {
        return needValNum;
    }

    public void setNeedValNum(int needValNum) {
        this.needValNum = needValNum;
    }
}
